package bavaria.hightech.accounting;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import bavaria.hightech.banking.Money;

/**
 * Saves all accounting entrys in a list, the list has several typical list operations such as add, next, toString
 * @author r4smus
 *
 */



public class AccountingList<T> implements Iterable<T>{
	public enum SortOption{
		MONEYSORT(new MoneyComparator()),DATESORT(new DateComparator()),REASONSORT(new ReasonComparator());
		
		private Comparator comp;
		
		private SortOption(Comparator comp){
			this.comp = comp;
		}
		
		private Comparator getComparator(){
			return comp;
		}
	}
	
	
	List<T> accountingList;
	private int counter;
	
	public AccountingList() {
		accountingList = new ArrayList<T>();
	}
	
	public void add(T b){
		accountingList.add(b);
	}
	
	public void clear(){
		accountingList.clear();
	}
	
	public T next(){
		if(counter >= accountingList.size()){
			return null;
		}
		T b = accountingList.get(counter);
		counter++;
		return b;
	}
	
	public void resetIteration(){
		counter = 0;
	}
	
	public String toString(){
		String s = "\n";
		for (T element : accountingList) {
			s += element.toString() +"\n";
		}
		return s;
	}

	@Override
	public Iterator<T> iterator() {
		return accountingList.iterator();
	}
	
	
	public void sort(SortOption option){
		Collections.sort(accountingList, option.getComparator());
	}

}
